import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testclusterrole:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/1_get_clusterrole_list.yaml'))
    def test_get_clusterrole_list(self, clusterroleinfo):
        # 获取clusterrole列表接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = base_url + clusterroleinfo['request']['url'].replace('{namespace}', 'default')
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/1_get_clusterrole_list_exception.yaml'))
    def test_get_clusterrole_list_exception(self, clusterroleinfo):
        # 获取clusterrole列表接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = base_url + clusterroleinfo['request']['url'].replace('{namespace}', 'default')
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/2_create_clusterrole.yaml'))
    def test_create_clusterrole(self, clusterroleinfo):
        # 创建clusterrole接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = base_url + clusterroleinfo['request']['url'].replace('{namespace}', 'default')
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        clusterrolename = req.json()['metadata']['name']
        globals()['clusterrolename'] = clusterrolename
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/2_create_clusterrole_exception.yaml'))
    def test_create_clusterrole_exception(self, clusterroleinfo):
        # 创建clusterrole接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = base_url + clusterroleinfo['request']['url'].replace('{namespace}', 'default')
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/3_get_specific_clusterrole.yaml'))
    def test_get_specific_clusterrole(self, clusterroleinfo):
        # 获取指定clusterrole接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/3_get_specific_clusterrole_exception.yaml'))
    def test_get_specific_clusterrole_exception(self, clusterroleinfo):
        # 获取指定clusterrole接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/4_update_clusterrole.yaml'))
    def test_update_clusterrole(self, clusterroleinfo):
        # 更新指定clusterrole接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        clusterroleinfo['request']['data'] = globals()['updatereqbody']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/4_update_clusterrole_exception.yaml'))
    def test_update_clusterrole_exception(self, clusterroleinfo):
        # 更新指定clusterrole接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        clusterroleinfo['request']['data'] = globals()['updatereqbody']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/5_update_section_clusterrole.yaml'))
    def test_update_section_clusterrole(self, clusterroleinfo):
        # 部分更新指定clusterrole的接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        header = clusterroleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/5_update_section_clusterrole_exception.yaml'))
    def test_update_section_clusterrole_exception(self, clusterroleinfo):
        # 部分更新指定clusterrole的接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        header = clusterroleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/6_delete_specific_clusterrole.yaml'))
    def test_delete_specific_clusterrole(self, clusterroleinfo):
        # 删除clusterrole接口-正常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrole
    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrole/6_delete_specific_clusterrole_exception.yaml'))
    def test_delete_specific_clusterrole_exception(self, clusterroleinfo):
        # 删除clusterrole接口-异常
        base_url = datas.baseUrl + Testclusterrole.url_unify
        url = (base_url + clusterroleinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{clusterrolename}', globals()['clusterrolename']))
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']
